﻿#pragma once
#include<iostream>
using namespace std;

enum Colo
{
	RED,
	BLACK
};

template<class T>
struct RBtreeNode
{
	T _data;
	RBtreeNode<T>* _left;
	RBtreeNode<T>* _right;
	RBtreeNode<T>* _parent;
	Colo _col;
	RBtreeNode(const T& data)
		:_data(data)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
	{

	}
};

template<class T>
struct RBtreeIterator
{
	typedef RBtreeNode<T> Node;
	typedef RBtreeIterator<T> self;

	Node* _node;
	RBtreeIterator(Node* node)
		:_node(node)
	{
	}
	T& operator*()
	{
		return _node->_data;
	}
	T* operator->()
	{
		return &_node->_data;
	}
	self operator++()
	{
		if (_node->_right)
		{
			Node* min = _node->_right;
			while (min->_left)
			{
				min = min->_left;
			}
			_node = min;
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && parent->_right == cur)
			{
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}
	bool operator!=(const self& s)
	{
		return _node != s._node;
	}
};

template<class K,class T,class bij>
class RBtree
{
	typedef RBtreeNode<T> Node;
public:
	typedef RBtreeIterator<T> Iterator;
	Iterator begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur);
	}
	Iterator end()
	{
		return Iterator(nullptr);
	}

	bool insert(const T& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}
		bij kot;
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (kot(kv) < kot(cur->_data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else if(kot(kv) > kot(cur->_data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		cur->_col = RED;
		if (kot(kv) < kot(parent->_data))
		{
			parent->_left = cur;
		}
		else
		{
			parent->_right = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grand = parent->_parent;
			if (grand->_left == parent)
			{
				Node* uncle = grand->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grand->_col = RED;

					//继续向上更新
					cur = grand;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_left == cur)
					{
						RotR(grand);
						grand->_col = RED;
						parent->_col = BLACK;
					}
					else
					{
						RotL(parent);
						RotR(grand);
						grand->_col = RED;
						cur->_col = BLACK;
					}
					//break;
				}
				
			}
			else
			{
				Node* uncle = grand->_left;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grand->_col = RED;

					cur = grand;
					parent = cur->_parent;
				}
				else
				{
					if (parent->_right == cur)
					{
						RotL(grand);
						grand->_col = RED;
						parent->_col = BLACK;
					}
					else
					{
						RotR(parent);
						RotL(grand);
						grand->_col = RED;
						cur->_col = BLACK;
					}
					//break;
				}
				
			}
		}
		_root->_col = BLACK;
		return true;

	}

	void RotR(Node* root)
	{
		Node* subL = root->_left;
		Node* subR = subL->_right;

		root->_left = subR;
		Node* root_parent = root->_parent;
		if (subR != nullptr)
		{
			subR->_parent = root;
		}

		subL->_right = root;
		root->_parent = subL;

		if (root_parent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (root_parent->_left == root)
			{
				root_parent->_left = subL;
			}
			else
			{
				root_parent->_right = subL;
			}
			subL->_parent = root_parent;
		}

	}
	void RotL(Node* root)
	{
		Node* subL = root->_right;
		Node* subR = subL->_left;

		root->_right = subR;
		if (subR != nullptr)
		{
			subR->_parent = root;
		}
		Node* root_parent = root->_parent;

		subL->_left = root;
		root->_parent = subL;
		if (root_parent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (root_parent->_left == root)
			{
				root_parent->_left = subL;

			}
			else
			{
				root_parent->_right = subL;
			}
		}
	}
	void print()
	{
		zho(_root);
	}

	bool kko(Node* root, int st1, int st2)
	{
		if (root == nullptr)
		{
			if (st1 != st2)
			{
				cout << "不是红黑树" << endl;
				return false;
			}
			cout << "当前路径的黑色数量:" << st1 << endl;
			return true;
		}
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "存在连续的红色节点" << endl;
			return false;
		}

		if (root->_col == BLACK)
		{
			st1++;
		}

		return kko(root->_left, st1, st2) && kko(root->_right, st1, st2);
	}

	bool cool()
	{
		if (_root == nullptr)
		{
			return true;
		}
		if (_root->_col == RED)
		{
			return false;
		}
		Node* cur = _root;
		int NOSL = 0;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				NOSL++;
			}
			cur = cur->_left;
		}
		
		return kko(_root,0,NOSL);
	}


	
protected:
	void zho(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		zho(root->_left);
		cout << root->_kv.first << ":" << root->_kv.second << endl;
		zho(root->_right);
	}
private:
	Node* _root = nullptr;
};